Ext.BLANK_IMAGE_URL = basePath + 'ext/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = basePath + 'ext/resources/images/default/s.gif';

/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.system.UserManage';
var status;

Ext.onReady(function(){
	Ext.QuickTips.init();

	var storereader= new Ext.data.JsonReader({
		//为分页增加
		root : 'results',
        totalProperty : 'totalCount',
        
    	fields:[
    		{name :'uid'},
    		{name :'name'},
    		{name :'username'},
    		{name :'password'},
			{name :'remarked'}
    	]
	});
		
	/**
	 * 查询分页数据
	 */
	var store = new Ext.data.Store({
		reader:storereader,
		proxy:new Ext.data.HttpProxy({
			url:clsUrl+'&methodName=getUser&paramCount=2'
		})
	});
     
	/**
	 * 分页Panel
	 */
	var toolpanel = new Ext.PagingToolbar({
                    pageSize:20,
                    store:store,
                    storeParams : {
                            className : 'cn.com.zsc.system.UserManage',
                            methodName : 'getUser',
                            paramCount : '2',
                            param_0 : '0',
                            param_1 : '20'
                        },
                        paramNames : {
                            start : 'param_0',
                            limit : 'param_1'
                        },
                    displayInfo: true,
                    displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                    emptyMsg: '没有记录'
                });
	
	/**
	 * 用户列表
	 */
	var basegrid = new Ext.grid.GridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,                                           
	  	enableHdMenu:false,
		frame :false,
		autoScroll :true,
		viewConfig: {
        	forceFit: true
    	},
    	columns: [
     	            new Ext.grid.RowNumberer(),
    	          	{id:'uid',name:'uid', header:'用户编号',enableColumnMove:false,sortable:true,hidden:true,hideCollapseTool:false, enableHdMenu:false,enableColumnResize :false,dataIndex: 'uid'},
    	          	{id:'name',name:'name', header:'用户名',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'name'},
    	          	{id:'username',name:'username',header:'登录名',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'username'},
    	          	{id:'password',name:'password',header:'密码',sortable:true,hidden:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false,dataIndex: 'password'},
    	          	{id:'remarked',name:'remarked',header:'备注',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'remarked'}
    	],
    	tbar:[
	    	     {
		    	     text:'新增',
		    	     iconCls:'add',
		      	   	 handler:function(){
		      	   	 	status=1;
		      	   	 	win.setTitle("新增用户");
		      	   	 	win.show();
		      	   	 	
						Ext.getCmp("name").setValue("");
						Ext.getCmp("username").setValue("");
						Ext.getCmp("loginpwd").setValue("");
						Ext.getCmp("repeatpwd").setValue("");
						Ext.getCmp("remarked").setValue("");
		    	     }
	      	   	},
	      	   	'-',
	      	   	{
		      	   	text:'修改',
		      	   	iconCls:'modify',
		      	   	handler:function(){
	      	   			status=2;
	      	   			var record = basegrid.getSelectionModel().getSelected(); 
	   		       		if(!record){   
			   		        Ext.Msg.show({
										title : '提示',
										width : 210,
										msg : '请选择要修改的用户!',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR
									});
						}else{
			      	   		if(record){
								win.setTitle("修改用户");
			            	   	win.show();
			            	   	
			            	   	var record = basegrid.getSelectionModel().getSelected();
								
								Ext.getCmp("uid").setValue(record.data.uid);
								Ext.getCmp("name").setValue(record.data.name);
								Ext.getCmp("username").setValue(record.data.username);
								Ext.getCmp("loginpwd").setValue(record.data.password);
								Ext.getCmp("repeatpwd").setValue(record.data.password);
								Ext.getCmp("remarked").setValue(record.data.remarked);
			            	}     
		      	   		}
	      	   		}
	      	   	},
	      	   	'-',
	      	   	{
		      	   	text:'删除',
		      	   	iconCls:'delete',
		      	   	handler:del
	      	   	}
	      	   	
 	  ],
 	  bbar:toolpanel
	});
	
	/**
	 * formPanel
	 */
	var formPanel = new Ext.FormPanel({
		bodyStyle:'padding:20px 15px 0',
		region:'center',
		layout:'form',
		frame:true,
		labelWidth:100,
		labelAlign:'right',
		items:[
		       {fieldLabel: '用户姓名',width:200,name:'name',id:'name',xtype:'textfield'},
		       {fieldLabel: '登录名',width:200,name:'username',id:'username',xtype:'textfield'},
		       {fieldLabel: '密码',width:200,name:'loginpwd',id:'loginpwd',xtype:'textfield',inputType :'password'},
		       {fieldLabel: '重复密码',width:200,name:'repeatpwd',id:'repeatpwd',xtype:'textfield',inputType :'password'},
			   {fieldLabel: '备注',width:200,name:'remarked',id:'remarked',xtype:'textarea',inputType :'textfield'},
			   {fieldLabel: '用户编号',width:200,name:'uid',id:'uid',xtype:'textfield',hideLabel:true,hidden:true}
		]
	});
	
	/**
	 * 保存取消按钮
	 */
	var win = new Ext.Window({
		title:'新增用户',
		width:400,
		height:300,
		layout:'border',
		closeAction:'hide',
		modal:true,
		draggable:false,
		resizable:false,
		tbar:[
		      {
		    	  text:'保存',
		    	  iconCls:'save',
		    	  handler:judge
		      },
		      '-',
		      {
		    	  text:'取消',
		    	  iconCls:'cancel',
		    	  handler:function(){
      	   			win.hide();
		      	  }
		      }
		],
		items:[formPanel]
	});
	
	var mainPanel = new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            split:true,
            border:false,
            frame:true,
            layout: 'border',  
            items:[basegrid]
    });
    
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [mainPanel]
	});
	
	/**
	 * 判断是新增还是修改
	 */
	function judge(){
		if(status==1){
			save();
		}else if(status==2){
			update();
		}
	}
	
	/**
	 * 新增用户
	 */
	function save(){
		if(!saveValidate()){
		    return;	
		}
			
		var obj = new Object();
		
		obj.name = Ext.getCmp("name").getValue();
		obj.username = Ext.getCmp("username").getValue();
		obj.password = Ext.getCmp("loginpwd").getValue();
		obj.remarked = Ext.getCmp("remarked").getValue();
		
		var url=clsUrl+'&methodName=insertUser&paramCount=0'
		
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    httpReq.onreadystatechange = onSaveRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onSaveRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',
		    	   		width:200,
		   				msg : '保存成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
					
		   			store.reload();
					Ext.getCmp("name").setValue("");
		   			Ext.getCmp("username").setValue("");
		   			Ext.getCmp("loginpwd").setValue("");
		   			Ext.getCmp("repeatpwd").setValue("");
					Ext.getCmp("remarked").setValue("");
					
		   			win.hide();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',
		    	   		width:200,
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	/**
	 * 修改用户
	 */
	function update(){
		if(!saveValidate()){
			return ;
		}
		
		var obj = new Object();
		
		obj.uid = Ext.getCmp("uid").getValue();
		obj.name = Ext.getCmp("name").getValue();
		obj.username = Ext.getCmp("username").getValue();
		obj.password = Ext.getCmp("loginpwd").getValue();
		obj.remarked = Ext.getCmp("remarked").getValue();
				
		var url=clsUrl+'&methodName=updateUser&paramCount=0'
		
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onUpdateRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onUpdateRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',
		    	   		width:200,
		   				msg : '保存成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
		   			store.reload();
		   					   			
		   			Ext.getCmp("name").setValue("");
		   			Ext.getCmp("username").setValue("");
		   			Ext.getCmp("loginpwd").setValue("");
		   			Ext.getCmp("repeatpwd").setValue("");
		   			win.hide();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',
		    	   		width:200,   
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	/**
	 * 删除用户
	 */
	function del(){
		var record = basegrid.getSelectionModel().getSelected();
		
		if(!record){
			Ext.Msg.show({
				title:'提示',
				width:210,
				msg:'请选择要删除的用户',
				buttons:Ext.Msg.OK,
				icon: Ext.Msg.ERROR 
			});	
		}	
		
		if(record){
			Ext.Msg.show({
				title:'提示',
				width:200,
				msg:'确定要删除用户！', 
				buttons: Ext.Msg.YESNO,
	            icon: Ext.MessageBox.QUESTION,
				fn:function(btn){
					if( "yes" == btn){
						delProc()
					}
				}
			});
			
		}
					
	}
	
	function delProc(){
		var record = basegrid.getSelectionModel().getSelected();
		 
		var obj = new Object();
		obj.uid = record.data.uid;
		
		var url=clsUrl+'&methodName=deleteUser&paramCount=0'
		
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onDelRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onDelRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',
		    	   		width:200,
		   				msg : '删除成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
		   			store.reload();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',   
		    	   		width:200,
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	/**
	 * 用户验证
	 */
	function saveValidate(){
		if(Ext.getCmp("name").getValue()==""){
			Ext.getCmp("name").markInvalid("用户姓名不能为空");
			return false;
		}
		
		if(Ext.getCmp("username").getValue()==""){
			Ext.getCmp("username").markInvalid("登录名不能为空");
			return false;
		}
		
		if(Ext.getCmp("loginpwd").getValue()==""){
			Ext.getCmp("loginpwd").markInvalid("密码不能为空");
			return false;
		}
		
		if(Ext.getCmp("loginpwd").getValue() != Ext.getCmp("repeatpwd").getValue()){
			Ext.getCmp("repeatpwd").markInvalid("密码不一致，请重新输入");
			return false;
		}
		return true;
	}
	
	store.on('beforeload',function(thiz,options){
		mainPanel.body.mask('正在加载数据','x-mask-loading');
    });
    
	store.on("load",function(){
		mainPanel.body.unmask();
	});
	
	store.load({
        params: {
            param_0: 0,
            param_1: 20
        }
    });
});

